package com.gk.carshopsys.controller;

import com.github.pagehelper.PageInfo;
import com.gk.carshopsys.annotation.Log;
import com.gk.carshopsys.pojo.Admin;
import com.gk.carshopsys.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.sql.Date;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    //初始页面跳转登录页
    @RequestMapping("/")
    public String tologin(){
      return "login";
    };

    //跳转登录页
    @RequestMapping("/login")
    public String login(){
        return "login";
    };

    //主页跳转
    @RequestMapping("/index")
    public String toindex(){return "index";}

    //登录页响应页面跳转
    @PostMapping ("/login.action")
    public String adminlogin(@RequestParam("user") String user,
                             @RequestParam("username") String username,
                             @RequestParam("password") String password, Model model, HttpSession session){

        Admin admin=adminService.loginAdmin(username,password);
        if(admin!=null){
            session.setAttribute("loginUser",username);
            return "redirect:/index";
        }else{
            model.addAttribute("msg", "密码或者账号有误,请重新输入");
            return "login";
        }
    } ;

    //退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login";
    };

    //管理员用户列表跳转
    @RequestMapping("/adminlist")
    public  String toadminlist(){
        return "adminlist";
    };

    //返回管理员用户列表
    @Log("查询管理员")
    @RequestMapping("/adminlist/query")
    @ResponseBody
    public List<Admin> findAdmin(@RequestBody Map<String,String> param){
        String username = param.get("username");
        String id_s = param.get("id");
        if (id_s!=null && !id_s.equals("")){
            try{
                Integer id = Integer.parseInt(id_s);
                return adminService.findAdminByIdAndUsername(id,username);
            }catch (ClassCastException e){
                return adminService.findAdminByIdAndUsername(null,username);
            }
        }else {
            return adminService.findAdminByIdAndUsername(null,username);
        }



    };

    //管理员用户添加
    //RedirectAttributesModelMap类防止重定向后model丢失
    @Log("添加管理员")
    @PostMapping("/adminlist/add")
    public  String adminlistadd(@RequestParam("add_adminname") String username,
                                @RequestParam("add_password") String password,RedirectAttributesModelMap model){

        int result = adminService.addAdmin(username,password);
        if(result==-1){
            model.addFlashAttribute("addmsg","插入失败，用户名已存在，请重新插入");
            return "redirect:/adminlist";
        }
        if(result==1){
            model.addFlashAttribute("addmsg","插入成功");
        }else{
            model.addFlashAttribute("addmsg","插入未知错误");
        }
            return "redirect:/adminlist";
        };


    // 管理员用户更新
    @Log("修改管理员")
    @PostMapping("/adminlist/upadte")
    public String adminlistupadte(@RequestParam("update_id") Integer id,
                                  @RequestParam("update_adminname") String username,
                                  @RequestParam("update_password") String password,RedirectAttributesModelMap model){
        int result=adminService.updateAdmin(id,username,password);
        if(result==-1){
            model.addFlashAttribute("updatemsg","修改失败，用户名已存在，请重新修改");
            return "redirect:/adminlist";
        }
        if (result==1){
            model.addFlashAttribute("updatemsg","修改成功");
        }else {
            model.addFlashAttribute("updatemsg","修改失败，未知错误");
        }
        return "redirect:/adminlist";

    };


    //管理员用户批量删除
    @Log("删除管理员")
    @ResponseBody
    @RequestMapping("/adminlist/del")
    public Map<String,Integer> adminlistdel(@RequestBody List<Integer> ids){

        int result = adminService.delAdmin(ids);
        Map<String,Integer> map = new HashMap<>();
        map.put("code",result);
        return map;
    }






}
